from ..attributes import *
from ..space import Position

from .grid import *


class Information(Object):
    @property
    def information(self):
        return {}
    
    @property
    def classes(self):
        return {}
    
    @property
    def organization(self):
        return Grid((Data(key) for key in sorted(self.information.keys())))
    
    @property
    def binding(self):
        binding = self.organization
        binding.bind(self)
        
        self.layout(binding)
        
        return binding
    
    def layout(self, organization):
        pass
    
    def display_with(self, primitive_source):
        return InformationDisplay(self, primitive_source)


class InformationPrimitiveSource(Object):
    renderer = Attribute()
    
    def create_text(self, element):
        pass

    def create_image(self, element):
        pass
    
    def create_button(self, element):
        pass


class InformationPrimitive(Object):
    element = Attribute()
    
    @property
    def classes(self):
        return self.element.classes
    
    @property
    def intrinsic_size(self):
        pass
    
    def set_area(self, area):
        pass


class InformationDisplay(Object):
    information = Attribute()
    primitives  = Attribute()
    
    binding        = Attribute(initiable = False)
    intrinsic_size = binding._.intrinsic_size
    add_class      = binding._.add_class
    add_classes    = binding._.add_classes
    destroy        = binding._.destroy
        
    def setup(self):
        self.binding = self.information.binding
        self.binding.create_elements_for(self.primitives)
    
    def set_area(self, area):
        self.binding.set_area(area)
    
    def set_default_area(self, dx = 0, dy = 0, mw = None, mh = None):
        width, height = self.intrinsic_size
        
        if mw is not None:
            width  = min(width,  mw)
        
        if mh is not None:
            height = min(height, mh)
        
        self.set_area(Position(dx, dy).extend_by(width, height))
